<?php

/***  Documentation

    Greqo Blogger Unit Test
    
    Revision: $Rev$
    Last Update: $Date: 2009-05-24 17:25:17 +0000 (Sun, 24 May 2009) $
    Author: Tom at klenwell@gmail.com
  
NOTES
    This test requires conf/login.php be set with valid Google account info
    for user with at least 1 Blogger blog

______________________________________________________________________________*/

#error_reporting(E_ALL);
$dn1 = dirname;
$ds = DIRECTORY_SEPARATOR;
$libdir = $dn1($dn1($dn1(__FILE__))) . $ds . 'lib' . $ds;
$grqdir = $dn1($libdir) . $ds . 'greqo' . $ds;
require_once($libdir . "simpletest{$ds}unit_tester.php");
require_once($grqdir . "blogger.class.php");

class BloggerClassTestCase extends UnitTestCase {
    
    var $debug = 0;
    var $is_set_up_once = 0;
    var $blogger_blog_id = '2399953';
    
    function setUp() 
    {
        $this->setUpOnce();    
    }
    
    function tearDown() 
    {
    }
    
    function setUpOnce()
    {
        if ( $this->is_set_up_once ) return;      
        
        // set up
        $utime = microtime();
        $this->start_timer = substr($utime,0,9) + substr($utime,-10);
        $this->is_set_up_once = 1;
        
        // Greqo class
        $this->Blogger = new GreqoBlogger($start_session=0);
    }
    
    

    /* TEST METHODS */
    function testTest() 
    {
        $this->assertTrue(1);
    }
    
    function testInstance()
    {        
        $this->assertTrue(is_a($this->Blogger, 'GreqoBlogger'));
    }
    
    function testParseBlogIdDev()
    {
        $id = 'tag:blogger.com,1999:user-000000.blog-111111';
        preg_match_all('%.*user-(\d+)\.blog-(\d+)$%iU', $id, $Matches);
        $this->assertEqual($Matches[1][0], '000000');
        $this->assertEqual($Matches[2][0], '111111');
    }
    
    function testFeedQueryBuilder()
    {
        $this->assertEqual(
            $this->Blogger->_feed_query(array('alt'=>'json')),
            '?alt=json' );
        
        // note: comma is encoded -> is this correct?
        $this->assertEqual(
            $this->Blogger->_feed_query(array('category'=>'one,two')),
            '?category=one%2Ctwo' );
        
        $this->assertEqual(
            $this->Blogger->_feed_query( array('category'=>
                array('and' => array('one', 'two')) ) ),
            '?category=one%2Ctwo' );
        
        $this->assertEqual(
            $this->Blogger->_feed_query( array('category'=>
                array('or' => array('one', 'two')) ) ),
            '?category=one%7Ctwo' );
        
        $this->assertEqual(
            $this->Blogger->_feed_query( array(
                'start-index' => 1,
                'max-results' => 25
            )),
            '?start-index=1&max-results=25' );
        
        $this->assertEqual(
            $this->Blogger->_feed_query(array('orderby'=>'updated')),
            '?orderby=updated' );
        
        $this->assertEqual(
            $this->Blogger->_feed_query( array(
                'published-min' => '2008-01-01',
                'published-max' => '2008-12-31'
            )),
            '?published-min=2008-01-01T00%3A00%3A00Z&published-max=2008-12-31T00%3A00%3A00Z' );
        
        $this->assertEqual(
            $this->Blogger->_feed_query( array( 'between' => array(
                '2008-01-01',
                '2008-12-31' )
            )),
            '?published-min=2008-01-01T00%3A00%3A00Z&published-max=2008-12-31T00%3A00%3A00Z' );
        
        $this->assertEqual(
            $this->Blogger->_feed_query( array(
                'orderby'     => 'updated',
                'updated-min' => '2008-01-01',
                'updated-max' => '2008-12-31'
            )),
            '?orderby=updated&updated-min=2008-01-01T00%3A00%3A00Z&updated-max=2008-12-31T00%3A00%3A00Z' );
        
        $ComplexQuery = array(
            'alt' => 'json',
            'category' => array(
                'and' => array('one','two','three')
            ),
            'offset' => 10,
            'limit' => 20,
            'published-max' => strtotime('2008-12-31')
        );
        $this->assertEqual(
            $this->Blogger->_feed_query($ComplexQuery),
            '?alt=json&category=one%2Ctwo%2Cthree&start-index=10&max-results=20&published-max=2008-12-31T00%3A00%3A00Z' );
    }
    
    
    
    function testParseOptionDict()
    {
        $OptionDict = array(
            'datetime'  => '2008-01-01 12:00:00',
            'tags'      => 'one,two,three',
            'null'      => 'invalid'
        );
        
        $Opts = $this->Blogger->_parse_option_dict($OptionDict);

        $this->assertEqual($Opts['datetime'], '2008-01-01T12:00:00Z');
        $this->assertEqual($Opts['categories'], $OptionDict['tags']);
        $this->assertFalse(isset($Opts['null']));
        #$this->pr($Opts);
    }
    
    function testEntryCategories()
    {
        $expect = <<<TXML
<category scheme="http://www.blogger.com/atom/ns#" term="marriage" />
<category scheme="http://www.blogger.com/atom/ns#" term="Mr. Darcy" />
TXML;
        $CategoryList = array('marriage', 'Mr. Darcy');
        $category_list_str = 'marriage, Mr. Darcy';
        
        $this->assertEqual(trim($this->Blogger->_entry_categories($CategoryList)),
                           trim($expect));
        $this->assertEqual(trim($this->Blogger->_entry_categories($category_list_str)),
                           trim($expect));
    }
    
    function testEntryXml()
    {
        $title = 'test';
        $body  = "It's a test of the Blogger & Google API";
        $Options['tags'] = 'one,two';
        $Options = $this->Blogger->_parse_option_dict($Options);
        $atom_xml = $this->Blogger->_entry_xml($title, $body, $Options);
        $this->assertTrue(strpos($atom_xml, "<content type='html'>It&apos;s a test of the Blogger &amp; Google API</content>"));
        $this->assertTrue(strpos($atom_xml, "<title type='html'>test</title>"));
    }
    
    function testPostResponse()
    {
        $xml = "<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href=\"http://www.blogger.com/styles/atom.css\" type=\"text/css\"?><entry xmlns='http://www.w3.org/2005/Atom' xmlns:georss='http://www.georss.org/georss'><id>tag:blogger.com,1999:blog-5735029764247244296.post-3738150438740509845</id><published>2009-03-21T23:55:00.001-07:00</published><updated>2009-03-21T23:55:22.244-07:00</updated><title type='text'>Greqo 4 Test</title><content type='html'>&lt;br /&gt;		&lt;p&gt;This is a test of the Greqo 4 Blogger Class&lt;/p&gt;&lt;br /&gt;&lt;p&gt;For more info, see &lt;a href=\"http://code.google.com/p/greqo/\"&gt;&lt;br /&gt;http://code.google.com/p/greqo/&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;	</content><link rel='replies' type='application/atom+xml' href='http://bohlag.blogspot.com/feeds/3738150438740509845/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=5735029764247244296&amp;postID=3738150438740509845' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5735029764247244296/posts/default/3738150438740509845'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5735029764247244296/posts/default/3738150438740509845'/><link rel='alternate' type='text/html' href='http://bohlag.blogspot.com/2009/03/greqo-4-test_9309.html' title='Greqo 4 Test'/><author><name>Greqo</name><uri>http://www.blogger.com/profile/16909144957327551013</uri><email>noreply@blogger.com</email></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry>";
        $Response = $this->Blogger->_post_response($xml);
        $this->assertEqual($Response['id'], 'tag:blogger.com,1999:blog-5735029764247244296.post-3738150438740509845');
        $this->assertEqual($Response['blog_num'], '5735029764247244296');
        $this->assertEqual($Response['id_num'], '3738150438740509845');
        $this->assertEqual($Response['published'], '2009-03-21T23:55:00.001-07:00');
        $this->assertEqual($Response['links']['replies'], 'http://bohlag.blogspot.com/feeds/3738150438740509845/comments/default');
        $this->assertEqual($Response['links']['view'], 'http://bohlag.blogspot.com/2009/03/greqo-4-test_9309.html');
    }
 


    /* HELPER METHODS */
    function get_script_time()
    {
        $utime = microtime();
        return number_format((substr($utime,0,9) + substr($utime,-10)) - $this->start_timer, 4);
    }

    function kill($html, $message='dying')
    {
        $this->print_d(htmlspecialchars($html), '#600');
        print $html;
        die($message);
    }
    
    function echo_d($message, $color='#ccc')
    {
        $_D = debug_backtrace();
        $f = basename($_D[0]['file']);
        $l = $_D[0]['line'];
        $loc = "{$f}:{$l}";
        $out = "<div style='line-height:1.5em; font-family:monospace; color:$color;'>$message <span style='color:#666;'>[$loc]</span></div>";
        $this->_DBG[] = "$loc -> " . strip_tags($message);
        echo $out;
        return;
    }
    
    function print_d($message, $color='#c33')
    {
        if ( $this->debug ) $this->echo_d($message, $color);
        return;
    }
    
    function print_r($Mixed)
    {
        $return = htmlspecialchars(print_r($Mixed, 1));
        $return = "<pre>$return</pre>";
        return $return;
    }
    
    function pr($Mixed, $header='')
    {
        if ( !empty($header) ) $header = "<b>$header</b>";
        $return = htmlspecialchars(print_r($Mixed, 1));
        $return = "<pre style='border:1px solid #ccc; background:#f3f3f3; padding:4px;'>$header\n\n$return</pre>";
        $this->echo_d($return, '#333');
    }
    
    function dump()
    {
        echo $this->print_r($this);
        return;
    }
}


$Test = &new BloggerClassTestCase();
$Test->run(new HtmlReporter());

$Test->echo_d('test complete in '.$Test->get_script_time().' s', '#600');
?>
